package com.tiankun.eurekasellgoods.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tiankun.eurekasellgoods.Utils.JsonResult;
import com.tiankun.eurekasellgoods.exception.BusinessException;
import com.tiankun.eurekasellgoods.pojo.Goods;
import com.tiankun.eurekasellgoods.pojo.Spike;
import com.tiankun.eurekasellgoods.service.GoodsService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import sun.security.krb5.internal.Ticket;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * User: shenhui
 * Date: 2020/11/11
 * Description
 * Version: V1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    GoodsService goodsService = null;
    @Resource
    RedisTemplate redisTemplate;

    @PostMapping("/getGoodsList")
    public Map getTkList(@RequestBody Map<String, Integer> map) throws Exception {

        if (map.get("page") != null) {
            PageHelper.startPage(map.get("page"), 10);
            List<Map<String, Object>> list = goodsService.getList(map.get("shopId"));
            PageInfo<Map<String, Object>> info = new PageInfo<>(list);
            System.out.println(info);
            System.out.println(info.getList());
            System.out.println(info.getEndRow());
            System.out.println(info.getTotal());
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(info);
            jsonResult.setCode("200");
            jsonResult.setMsg("这是分页的商品列表");
            return jsonResult.getValues();

        } else {
            throw new BusinessException("请输入当前页page");
        }

    }

    @PostMapping("/getGoodsNameLike")
    public Map getNameLike(@RequestBody Map<String, Object> map) throws Exception {

        if (map.get("page") != null) {
            PageHelper.startPage((Integer) map.get("page"), 10);
            List<Map<String, Object>> shopId = goodsService.goodsNameList((Integer) map.get("shopId"), map.get("godName").toString());
            PageInfo<Map<String, Object>> info = new PageInfo<>(shopId);
            System.out.println(info);
            System.out.println(info.getList());
            System.out.println(info.getEndRow());
            System.out.println(info.getTotal());
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(info);
            jsonResult.setCode("200");
            jsonResult.setMsg("这是分页的商品名模糊查询");
            return jsonResult.getValues();

        } else {
            throw new BusinessException("请输入当前页page");
        }

    }

    @PostMapping("/goodsStateList")
    public Map getStateList(@RequestBody Map<String, Integer> map) throws Exception {

        if (map.get("page") != null) {
            PageHelper.startPage(map.get("page"), 10);
            List<Map<String, Object>> list = goodsService.goodsStateList(map.get("shopId"), map.get("godState"));
            PageInfo<Map<String, Object>> info = new PageInfo<>(list);
            System.out.println(info);
            System.out.println(info.getList());
            System.out.println(info.getEndRow());
            System.out.println(info.getTotal());
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(info);
            jsonResult.setCode("200");
            jsonResult.setMsg("这是分页的搜索商品状态列表");
            return jsonResult.getValues();

        } else {
            throw new BusinessException("请输入当前页page");
        }
    }

    @PostMapping("/goodsTypeList")
    public Map getTypeList(@RequestBody Map<String, Integer> map) throws Exception {

        if (map.get("page") != null) {
            PageHelper.startPage(map.get("page"), 10);
            List<Map<String, Object>> maps = goodsService.goodsTypeList(map.get("shopId"), map.get("godType"));
            PageInfo<Map<String, Object>> info = new PageInfo<>(maps);
            System.out.println(info);
            System.out.println(info.getList());
            System.out.println(info.getEndRow());
            System.out.println(info.getTotal());
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(info);
            jsonResult.setCode("200");
            jsonResult.setMsg("这是分页的搜索商品类型列表");
            return jsonResult.getValues();

        } else {
            throw new BusinessException("请输入当前页page");
        }
    }

    @PostMapping("/deleteGoods")
    public Map DeletGoods(@RequestBody Map<String, Integer> map) throws Exception {
        Integer godId = goodsService.delete(map.get("godId"));
        JsonResult jsonResult = new JsonResult();
        if (godId == 1) {

            jsonResult.setData(godId);
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");

        } else {
            throw new BusinessException("删除失败，请重试");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/updateGoodsState")
    public Map updateGoodsState(@RequestBody Map<String, Integer> map) throws Exception {
        Integer integer = goodsService.updateGoodsState(map.get("godId"), map.get("godState"));
        JsonResult jsonResult = new JsonResult();
        if (integer == 1) {
            jsonResult.setCode("200");
            jsonResult.setData(integer);
            jsonResult.setMsg("修改商品状态成功");

        } else {
            throw new BusinessException("修改失败，请稍后重试");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/updateGoodsNumber")
    public Map updateGoodsNumber(@RequestBody Map<String, Integer> map) throws Exception {
        Integer integer = goodsService.updateGoodsNumber(map.get("godId"), map.get("godNumber"));
        JsonResult jsonResult = new JsonResult();
        if (integer == 1) {
            jsonResult.setCode("200");
            jsonResult.setData(integer);
            jsonResult.setMsg("修改库存成功");
        } else {
            throw new BusinessException("修改失败，请稍后再试");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/goodsDetails")
    public Map goodsDetails(@RequestBody Map<String, Integer> map) throws Exception {
        List<Map<String, Object>> godId = goodsService.goodsDetails(map.get("godId"));
        goodsService.GoodsCount(map.get("godId"));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(godId);
        jsonResult.setMsg("这是商品详情json");
        return jsonResult.getValues();
    }

    @PostMapping("/goodsAdd")
    public Map add(@RequestBody Map<String, Object> map) {
        JsonResult jsonResult = new JsonResult();

        Goods goods = JSON.parseObject(JSON.toJSONString(map), Goods.class);
        if (goods.getGodName() != null && !goods.getGodName().equals("") && goods.getGodMoney() != null && !goods.getGodMoney().equals("") && goods.getGodNumber() != null && !goods.getGodNumber().equals("")) {
            goods.setGodState(0);
            Integer add = goodsService.add(goods);
            Map<String, Object> id = goodsService.getId();
            Integer godId = (Integer) id.get("god_id");
            goodsService.addGoodsImages(godId, map.get("giUrl").toString());
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");

        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/goodsHot")
    public Map goodsHot() throws Exception {
        List<Map<String, Object>> godId = goodsService.goodsHot();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(godId);
        jsonResult.setMsg("这是热度商品json");
        return jsonResult.getValues();
    }

    @PostMapping("/goodsSpike")
    public Map goodsSpike(@RequestBody Map<String, Integer> map) throws Exception {
//        List<Map<String, Object>> godId = goodsService.goodsSpike(map.get("shopId"));
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setCode("200");
//        jsonResult.setData(godId);
//        jsonResult.setMsg("这是商品秒杀管理列表");
//        return jsonResult.getValues();

        if (map.get("page") != null) {
            PageHelper.startPage(map.get("page"), 10);
            List<Map<String, Object>> godId = goodsService.goodsSpike(map.get("shopId"));
            PageInfo<Map<String, Object>> info = new PageInfo<>(godId);
            System.out.println(info);
            System.out.println(info.getList());
            System.out.println(info.getEndRow());
            System.out.println(info.getTotal());
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(info);
            jsonResult.setCode("200");
            jsonResult.setMsg("这是分页的商品秒杀管理列表");
            return jsonResult.getValues();

        } else {
            throw new BusinessException("请输入当前页page");
        }
    }

    @PostMapping("/goodsUpdate")
    public Map goodsUpdate(@RequestBody Goods goods) {
        JsonResult jsonResult = new JsonResult();
        if (goods.getGodId() != null && !goods.getGodId().equals("")) {
            Integer integer = goodsService.updateGoods(goods);
            jsonResult.setCode("200");
            jsonResult.setData(integer);
            jsonResult.setMsg("修改成功");
        } else {
            throw new BusinessException("修改失败，请稍后重试");

        }
        return jsonResult.getValues();
    }

    @PostMapping("/goodsSpikeDetails")
    public Map goodsSpikeDetails(@RequestBody Map<String, Integer> map) throws Exception {
        List<Map<String, Object>> spkId = goodsService.goodsSpikeDetails(map.get("spkId"));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(spkId);
        jsonResult.setMsg("这是商品秒杀管理详情");
        return jsonResult.getValues();
    }

    @PostMapping("/goodsSpikeUpdate")
    public Map goodsSpikeUpdate(@RequestBody Spike spike) {
        JsonResult jsonResult = new JsonResult();

        if (spike.getSpkId() != null && !spike.getSpkId().equals("")) {
            Integer integer = goodsService.updateSpike(spike);
            jsonResult.setCode("200");
            jsonResult.setData(integer);
            jsonResult.setMsg("修改成功");
        } else {
            throw new BusinessException("修改失败，请稍后重试");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/nameSpike")
    public Map nameSpike(@RequestBody Map<String, Object> map) throws Exception {

        if (map.get("page") != null) {
            PageHelper.startPage((Integer) map.get("page"), 10);
            List<Map<String, Object>> shopId = goodsService.nameSpike((Integer) map.get("shopId"), map.get("godName").toString());
            PageInfo<Map<String, Object>> info = new PageInfo<>(shopId);
            System.out.println(info);
            System.out.println(info.getList());
            System.out.println(info.getEndRow());
            System.out.println(info.getTotal());
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(info);
            jsonResult.setCode("200");
            jsonResult.setMsg("这是分页的秒杀管理商品名模糊查询");
            return jsonResult.getValues();

        } else {
            throw new BusinessException("请输入当前页page");
        }

    }

    @PostMapping("/typeSpike")
    public Map typeSpike(@RequestBody Map<String, Integer> map) throws Exception {

        if (map.get("page") != null) {
            PageHelper.startPage(map.get("page"), 10);
            List<Map<String, Object>> maps = goodsService.typeSpike(map.get("shopId"), map.get("godType"));
            PageInfo<Map<String, Object>> info = new PageInfo<>(maps);
            System.out.println(info);
            System.out.println(info.getList());
            System.out.println(info.getEndRow());
            System.out.println(info.getTotal());
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(info);
            jsonResult.setCode("200");
            jsonResult.setMsg("这是分页的搜索秒杀商品类型列表");
            return jsonResult.getValues();

        } else {
            throw new BusinessException("请输入当前页page");
        }
    }

    @PostMapping("/stateSpike")
    public Map stateSpike(@RequestBody Map<String, Integer> map) throws Exception {
        List<Map<String, Object>> shopId = goodsService.stateSpike(map.get("shopId"));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(shopId);
        jsonResult.setCode("200");
        jsonResult.setMsg("这是添加秒杀商品的已上架下拉框");
        return jsonResult.getValues();

    }

    @PostMapping("/insetSpike")
    public Map insertSpike(@RequestBody Spike spike) throws Exception {
        JsonResult jsonResult = new JsonResult();
        if (spike.getGodId() != null && !spike.getGodId().equals("")) {
            Integer integer = goodsService.insertSpike(spike);
            jsonResult.setData(integer);
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
            Map<String, Object> spikeId = goodsService.getSpikeId();
            String name = "stock" + spikeId.get("spk_id");
            Integer number = (Integer) spikeId.get("spk_number");
            ValueOperations valueOperations = redisTemplate.opsForValue();
            valueOperations.set(name, number);
        } else {
            throw new BusinessException("添加失败");

        }
        return jsonResult.getValues();

    }

    @PostMapping("/moveGoodsDetails")
    public Map moveGoodsDetails(@RequestBody Map<String, Integer> map) throws Exception {
        List<Map<String, Object>> godId = goodsService.moveGoodsDetails(map.get("godId"));

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(godId);
        jsonResult.setMsg("这是移动端商品详情json");
        return jsonResult.getValues();
    }

    @PostMapping("/moveAscSpike")
    public Map moveAscSpike() {
        List<Map<String, Object>> maps = goodsService.moveAscSpike();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.setMsg("这是移动端按开始时间排序的秒杀商品");
        return jsonResult.getValues();

    }

    @PostMapping("/timeSpike")
    public Map timeSpike(@RequestBody Map<String, String> map) throws Exception {

        List<Map<String, Object>> maps = goodsService.timeSpike(map.get("startDate"), map.get("endDate"));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.setMsg("这是移动端按时间查询的秒杀商品");
        return jsonResult.getValues();

    }

    @PostMapping("/typeAll")
    public Map typeAll() throws Exception {

        List<Map<String, Object>> maps = goodsService.typeAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.setMsg("这是商品类型下拉框");
        return jsonResult.getValues();

    }

    @PostMapping("/deleteSpike")
    public Map deleteSpike(@RequestBody Map<String, Integer> map) throws Exception {

        Integer spkId = goodsService.deleteSpike(map.get("spkId"));
        JsonResult jsonResult = new JsonResult();
        if (spkId == 1) {
            jsonResult.setCode("200");
            jsonResult.setData(spkId);
            jsonResult.setMsg("删除秒杀商品成功");
        } else {
            throw new BusinessException("删除失败");
        }
        return jsonResult.getValues();

    }
}
